package com.fram.service.api.v1.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.api.volume.request.VolumeAnswerRequest;
import com.fram.bean.api.volume.response.VolumeResponse;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.BeanMapper;
import com.fram.entity.VolumeAnswer;
import com.fram.entity.VolumePaper;
import com.fram.mapper.VolumeAnswerMapper;
import com.fram.mapper.VolumePaperMapper;
import com.fram.service.api.v1.VolumeService;

@Service
public class VolumeServiceImpl implements VolumeService {

	@Autowired
	private VolumeAnswerMapper volumeAnswerMapper;
	@Autowired
	private VolumePaperMapper volumePaperMapper;

	/** 根据版本 展示 问题 **/
	@Override
	public List<VolumeResponse> getVolumePaperList(String version,Long userId) throws ProException {
		VolumeAnswer request = new VolumeAnswer();
		request.setVolumeEdition(version);
		request.setUserId(userId);
		if(volumeAnswerMapper.selectByCount(request) !=null) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(),"改版本的问题已经回答过不可再回答");
		}
		List<VolumeResponse> list = volumePaperMapper.getVolumePaperList(version);
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	/** 点击提交 （选择题 以及 问答题 用户 提交到这个表里 如5到题 都上传） **/
	@Override
	public int insertAnswer(List<VolumeAnswerRequest> request) throws ProAffairException {
		int in = 0;
		List<VolumeAnswer> listData = new ArrayList<VolumeAnswer>();
		for (VolumeAnswerRequest volumeAnswerRequest : request) {
			if (volumeAnswerRequest.getAnswerType() != 2) {
				// 遍历请求中的 选项 选择
				for (Long optionId : volumeAnswerRequest.getOptionIds()) {
					if(optionId == null ) {
						throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
					}
					// new 新请求 ，几个optionId 就几个增加
					VolumeAnswer data = BeanMapper.map(volumeAnswerRequest, VolumeAnswer.class);
					data.setOptionId(optionId);
					listData.add(data);
				}
			} else {
				if(volumeAnswerRequest.getContent() == null || volumeAnswerRequest.getContent().equals("")) {
					throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
				}
				VolumeAnswer data = BeanMapper.map(volumeAnswerRequest, VolumeAnswer.class);
				listData.add(data);
			}
		}
		if (volumeAnswerMapper.insertSelectiveByList(listData) != listData.size()) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		return in;
	}

	@Override
	public List<VolumePaper> ListApiEdition()throws ProException {
		List<VolumePaper> list = volumePaperMapper.selectByEdition();
		if(list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}
}
